package com.punch.oauth.mobile;


import com.punch.oauth.service.impl.UaaUserServiceImpl;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * @author mall
 */
public class MobileCodeAuthenticationProvider implements AuthenticationProvider {


    private UaaUserServiceImpl userDetailsService;

    @Override
    public Authentication authenticate(Authentication authentication) {
        MobileCodeAuthenticationToken authenticationToken = (MobileCodeAuthenticationToken) authentication;
        String mobile = (String) authenticationToken.getPrincipal();
        UserDetails user = userDetailsService.loadUserByUsername(mobile);
        MobileCodeAuthenticationToken authenticationResult = new MobileCodeAuthenticationToken(user, user.getAuthorities());
        authenticationResult.setDetails(authenticationToken.getDetails());
        return authenticationResult;
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return MobileCodeAuthenticationToken.class.isAssignableFrom(authentication);
    }

//    public ZltUserDetailsService getUserDetailsService() {
//        return userDetailsService;
//    }

    public void setUserDetailsService(UaaUserServiceImpl userDetailsService) {
        this.userDetailsService = userDetailsService;
    }
}
